home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / domacnost a kancelar / joomla / Joomla_1.5.4-Stable-Full_Package.exe / plugins / authentication / gmail.php < prev    next >
PHP Script  |  2008-07-06  |  3KB  |  107 lines

  1. <?php
  2. /**
  3.  * @version        $Id: gmail.php 10396 2008-06-05 19:19:55Z willebil $
  4.  * @package        Joomla
  5.  * @subpackage    JFramework
  6.  * @copyright    Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  7.  * @license        GNU/GPL, see LICENSE.php
  8.  * Joomla! is free software. This version may have been modified pursuant
  9.  * to the GNU General Public License, and as distributed it includes or
  10.  * is derivative of works licensed under the GNU General Public License or
  11.  * other free or open source software licenses.
  12.  * See COPYRIGHT.php for copyright notices and details.
  13.  */
  14.  
  15. // Check to ensure this file is included in Joomla!
  16. defined('_JEXEC') or die( 'Restricted access' );
  17.  
  18. jimport( 'joomla.plugin.plugin' );
  19.  
  20. /**
  21.  * GMail Authentication Plugin
  22.  *
  23.  * @author Samuel Moffatt <sam.moffatt@joomla.org>
  24.  * @package        Joomla
  25.  * @subpackage    JFramework
  26.  * @since 1.5
  27.  */
  28. class plgAuthenticationGMail extends JPlugin
  29. {
  30.     /**
  31.      * Constructor
  32.      *
  33.      * For php4 compatability we must not use the __constructor as a constructor for plugins
  34.      * because func_get_args ( void ) returns a copy of all passed arguments NOT references.
  35.      * This causes problems with cross-referencing necessary for the observer design pattern.
  36.      *
  37.      * @param object $subject The object to observe
  38.      * @param     array  $config  An array that holds the plugin configuration
  39.      * @since 1.5
  40.      */
  41.     function plgAuthenticationGMail(& $subject, $config) {
  42.         parent::__construct($subject, $config);
  43.     }
  44.  
  45.     /**
  46.      * This method should handle any authentication and report back to the subject
  47.      *
  48.      * @access    public
  49.      * @param   array     $credentials Array holding the user credentials
  50.      * @param     array   $options     Array of extra options
  51.      * @param    object    $response    Authentication response object
  52.      * @return    boolean
  53.      * @since 1.5
  54.      */
  55.     function onAuthenticate( $credentials, $options, &$response )
  56.     {
  57.         $message = '';
  58.         $success = 0;
  59.         if(function_exists('curl_init'))
  60.         {
  61.             if(strlen($credentials['username']) && strlen($credentials['password']))
  62.             {
  63.                 $curl = curl_init('https://mail.google.com/mail/feed/atom');
  64.                 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  65.                 //curl_setopt($curl, CURLOPT_HEADER, 1);
  66.                 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  67.                 curl_setopt($curl, CURLOPT_USERPWD, $credentials['username'].':'.$credentials['password']);
  68.                 $result = curl_exec($curl);
  69.                 $code = curl_getinfo ($curl, CURLINFO_HTTP_CODE);
  70.  
  71.                 switch($code)
  72.                 {
  73.                     case 200:
  74.                          $message = 'Access Granted';
  75.                          $success = 1;
  76.                     break;
  77.                     case 401:
  78.                         $message = 'Access Denied';
  79.                     break;
  80.                     default:
  81.                         $message = 'Result unknown, access denied.';
  82.                         break;
  83.                 }
  84.             }
  85.             else  {
  86.                 $message = 'Username or password blank';
  87.             }
  88.         }
  89.         else {
  90.             $message = 'curl isn\'t installed';
  91.         }
  92.  
  93.         if ($success)
  94.         {
  95.             $response->status          = JAUTHENTICATE_STATUS_SUCCESS;
  96.             $response->error_message = '';
  97.             $response->email     = $credentials['username'];
  98.             $response->fullname = $credentials['username'];
  99.         }
  100.         else
  101.         {
  102.             $response->status         = JAUTHENTICATE_STATUS_FAILURE;
  103.             $response->error_message    = 'Failed to authenticate: ' . $message;
  104.         }
  105.     }
  106. }
  107.